<?php
//Deal expires at 12 pm
include '../../../wp-blog-header.php';

//$url =  'http://www.feed43.com/'.$_GET['url'];
//$city = $_GET['city'];


$brand_id = '195';
//$rss = fetch_rss($url);

$url = 'http://www.mydala.com/alliance/deals.php?KG=BUZZR';
$count = 0;
function download_page($path){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL,$path);
        curl_setopt($ch, CURLOPT_FAILONERROR,1);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
        curl_setopt($ch, CURLOPT_TIMEOUT, 15);
        $retValue = curl_exec($ch);                      
        curl_close($ch);
        return $retValue;
}

$curl_json_output = download_page($url);
//$mydala_deals = xml2array($curl_json_output);
//$mydala_deals = array_slice($mydala_deals['DEAL']['EVENT'], 0, 4);

//echo "<pre>";
//print_r($mydala_deals['DEAL']['EVENT']);
//exit();

foreach($mydala_deals['DEAL']['EVENT'] as $key => $value)
  {
    $columns_deal['name'] = pods_sanitize($value['DEAL_HEADING']);
    $columns_deal['camp_link'] = substr(sanitize_title($columns_deal['name']),0,128);
    $ext_img_url = $value['IMG_URL'];
    $deal = new Pod('deal');
    $where = 't.camp_link = "'.$columns_deal['camp_link'].'" AND t.daily = 1';
    $deal->findRecords('id DESC', 1,$where);
    $dealsFound = $deal->getTotalRows();
	$city = $value['CITY_NAME'];
	if($city == 'New Delhi'){
		$city = 'Delhi';
	}
	if($city == 'Bangalore'){
		$city = 'Bengaluru';
	}
		
    if($dealsFound == 0)
    {
		$count++;
      $city_pod = new Pod('city');
      $city_pod->page=1;
      $city_pod->search=false;
    	$city_pod->findRecords("name ASC",1,"t.name = '".$city."'");
    	while($city_pod->fetchRecord())
    	{
      	$city_id  = $city_pod->get_field('id');
    	}
      $columns_deal['city'] = $city_id;
      
      $extlink = $value['DEAL_URL'];
      $shortened_url = make_bitly_url($extlink);
      if(empty($shortened_url))
      {
        $shortened_url = make_goog_url($extlink);
      }
      if(empty($shortened_url))
      {
        $columns_deal['extlink'] = $value['DEAL_URL']; 
      }
      else
	  {
        $columns_deal['extlink'] = $shortened_url;
      }
      
      $columns_deal['camp_end'] = date('Y-m-d 13:00:00',strtotime($value['DEAL_END_DATE']));
      $columns_deal['camp_start'] = date('Y-m-d 22:00:00',strtotime($value['DEAL_START_DATE']));
      $columns_deal['brand'] = $brand_id;
      $columns_deal['daily'] = '1';
	  if(!empty($value['PRICE'])){
      $columns_deal['oriprice'] = $value['PRICE'];
	  }
	  $columns_deal['disprice'] = $value['TOTAL_PRICE'];
	  $columns_deal['disper'] = $value['DISCOUNT'];
	  $columns_deal['short'] = pods_sanitize(strip_tags($value['THE_DEAL']));
      
          
      $columns_deal['ext_image_url'] = $ext_img_url;
    
      $cat_id = check_daily_category($columns_deal['name']);
      if(!empty($cat_id))
      {
        $columns_deal['camp_cat'] = $cat_id;
      }
      
      $api = new PodAPI();
      $params = array('datatype' => 'deal', 'columns' => $columns_deal);
    
      
     // echo '<pre>';
     // print_r($params);
     // echo '</pre>';
       
      $api->save_pod_item($params);
	  if($count == 4){
		exit();
	  }
	
    }
    
  }

?>
